// pages/information/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    real_name: '',
    phone_number: '',
    identity_index: 0,
    identity_arr: ['学生', '教职工'],
    college_index: null,
    college_arr: [],
    pro_index: null,
    pro_arr: []

  },
  nameChange: function(e){
    this.setData({
      real_name: e.detail.value
    })
  },
  phoneChange: function(e){
    this.setData({
      phone_number: e.detail.value
    })
  },
  getUser() {
    var userId = wx.getStorageSync('userInfo')['id'];
    app.data.utils.request({
      url: app.data.api.user+ userId + ".api",
        data: {
        },
        method: 'get',
        success: (data) => {
          console.log(data);
          for(var index in this.data.pro_arr){
            if(data.level_id == this.data.pro_arr[index].id){
              this.setData({
                pro_index: index,
              })
            }
          }
          for(var index in this.data.college_arr){
            if(data.school_id == this.data.college_arr[index].id){
              this.setData({
                college_index: index,
              })
            }
          }
          for(var index in this.data.identity_arr){
            if(data.user_type == this.data.identity_arr[index]){
              this.setData({
                identity_index: index,
              })
            }
          }
          this.setData({
            phone_number:data.phone,
            real_name:data.real_name
          })
        }
    });
  },
  getSchool(){
    app.data.utils.request({
      url: app.data.api.schools,
        data: {
        },
        method: 'get',
        success: (data) => {
          console.log(data);
          this.setData({
            college_arr:data
          })
        }
    });
  },
  getLevels(){
    app.data.utils.request({
      url: app.data.api.levels,
        data: {
        },
        method: 'get',
        success: (data) => {
          console.log(data);
          this.setData({
            pro_arr:data
          })
        }
    });
  },
  collChange: function(e){
    console.log(e);
    this.setData({
      college_index: e.detail.value,
    })
  },
  identityChange: function(e){
    console.log(e);
    this.setData({
      identity_index: e.detail.value,
    })

  },
  proChange: function(e){
    this.setData({
      pro_index: e.detail.value,
    })
  },
  submit: function(e){
    //提交formId
    var userId = wx.getStorageSync('userInfo')['id'];
    app.data.utils.request({
      url: app.data.api.formID + "/" + userId + "/formID.api",
      data: {form_id: e.detail.formId},
      method: 'post',
    })
    if(!this.data.real_name){
      wx.showToast({
        title: '真实姓名不能为空',
        icon: 'none'
      })
      return;
    }
    console.log(this.data.phone_number);
    if(!this.data.phone_number){
      wx.showToast({
        title: '电话号码不能为空',
        icon: 'none'
      })
      return;
    }
    if(this.data.identity_index == null){
      wx.showToast({
        title: '必须选择身份',
        icon: 'none'
      })
      return;
    }
    if(!this.data.college_index){
      wx.showToast({
        title: '学院不能为空',
        icon: 'none'
      })
      return;
    }
    if(!this.data.pro_index){
      wx.showToast({
        title: '专业不能为空',
        icon: 'none'
      })
      return;
    }
    var userId = wx.getStorageSync('userInfo')['id'];
    app.data.utils.request({
      url: app.data.api.user+ userId +".api",
        data: {
          phone:this.data.phone_number,
          real_name:this.data.real_name,
          school: this.data.college_arr[this.data.college_index].name,
          school_id: this.data.college_arr[this.data.college_index].id,
          level: this.data.pro_arr[this.data.pro_index].name,
          level_id: this.data.pro_arr[this.data.pro_index].id,
        },
        method: 'post',
        success: (data) => {
          wx.showToast({
            title: '提交成功',
          })
         this.getUser();
        }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getUser();
      this.getSchool();
      this.getLevels();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})